package it.gestionedocumentale.struts.action.admin.tag;

import it.gestionedocumentale.model.Tag;
import it.gestionedocumentale.model.Utente;
import it.gestionedocumentale.spring.service.MyServiceFactory;
import it.gestionedocumentale.struts.form.TagForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.BeanUtils;

public class PrepareCRUDTagAction extends Action {
	private static Log log = LogFactory.getLog(PrepareCRUDTagAction.class);
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Utente utenteSession = (Utente) request.getSession().getAttribute("utenteSession");
		if(utenteSession == null) {
			return mapping.findForward("notInSession");
		}
		if(!utenteSession.getGruppo().getCodGruppo().equals("ADMIN")) {
			return mapping.findForward("notAllowed");
		}
		TagForm form2 = (TagForm) form;
		String codOp = form2.getCodOp();
		if(codOp.equals("inserisci")) {
			return mapping.findForward("inserisci");
		}
		Tag tag = null;
		try {
			tag = MyServiceFactory.getTagService().findById(form2.getIdTag());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			return mapping.findForward("error");
		}
		BeanUtils.copyProperties(tag, form2);
		if(codOp.equals("modifica")) {
			return mapping.findForward("modifica");
		}
		return mapping.findForward("cancella");
	}

}
